package com.glorysoft.xztpda.ui.lotinfo

import android.content.Intent
import android.os.Bundle
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityLotQueryBinding
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import kotlinx.android.synthetic.main.activity_lot_query.etLotID
import kotlinx.android.synthetic.main.activity_lot_query.ivLotScan
/**
 * FileName: LotQueryActivity
 * Author: XJH
 * Date: 2022/7/5 17:30
 * Description:
 * History:
 */
class LotQueryActivity : BaseActivity<LotQueryViewModel,ActivityLotQueryBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_lot_query
    }

    override fun initVariableId(): Int {
        return BR.lotQueryViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("批次查询")

        initEdit(etLotID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getLotInfo(s)
            }

        })

        ivLotScan.setOnClickListener{
            startScanForCode(this,10001)
        }
    }

    override fun initData() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK){
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when(requestCode){
                        10001 -> {
                            etLotID.setText(barCode)
                            viewModel.getLotInfo(barCode)
                        }
                    }

                }
            }
        }
    }
}